# -*- coding: utf-8 -*-
"""
响应工具类
"""
from typing import Any, Dict, Optional
from datetime import datetime
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse

class ResponseUtil:
    """统一响应格式工具类"""
    
    @staticmethod
    def success(data: Any = None, message: str = "操作成功", code: int = 200) -> Dict[str, Any]:
        """成功响应"""
        return {
            "code": code,
            "message": message,
            "data": jsonable_encoder(data),
            "timestamp": datetime.now().isoformat(),
            "success": True
        }
    
    @staticmethod
    def error(message: str = "操作失败", code: int = 500, data: Any = None) -> Dict[str, Any]:
        """错误响应"""
        return {
            "code": code,
            "message": message,
            "data": data,
            "timestamp": datetime.now().isoformat(),
            "success": False
        }
    
    @staticmethod
    def paginate(data: list, total: int, page: int = 1, page_size: int = 10, 
                message: str = "查询成功") -> Dict[str, Any]:
        """分页响应"""
        return {
            "code": 200,
            "message": message,
            "data": {
                "list": data,
                "pagination": {
                    "total": total,
                    "page": page,
                    "page_size": page_size,
                    "total_pages": (total + page_size - 1) // page_size
                }
            },
            "timestamp": datetime.now().isoformat(),
            "success": True
        }
    
    @staticmethod
    def unauthorized(message: str = "未授权访问") -> Dict[str, Any]:
        return ResponseUtil.error(message=message, code=401)
    
    @staticmethod
    def forbidden(message: str = "访问被禁止") -> Dict[str, Any]:
        return ResponseUtil.error(message=message, code=403)
    
    @staticmethod
    def not_found(message: str = "资源不存在") -> Dict[str, Any]:
        return ResponseUtil.error(message=message, code=404)
    
    @staticmethod
    def bad_request(message: str = "请求参数错误") -> Dict[str, Any]:
        return ResponseUtil.error(message=message, code=400)

def success_response(data: Any = None, message: str = "操作成功", code: int = 200) -> Dict[str, Any]:
    """成功响应"""
    return {
        "code": code,
        "message": message,
        "data": data
    }

def error_response(message: str = "操作失败", code: int = 400, data: Any = None) -> Dict[str, Any]:
    """错误响应"""
    return {
        "code": code,
        "message": message,
        "data": data
    }

def json_response(data: Any = None, message: str = "操作成功", code: int = 200) -> JSONResponse:
    """返回JSON响应"""
    response_data = {
        "code": code,
        "message": message,
        "data": data
    }
    return JSONResponse(content=response_data)

def pagination_response(items: list, total: int, page: int, page_size: int, message: str = "获取数据成功") -> Dict[str, Any]:
    """分页响应"""
    total_pages = (total + page_size - 1) // page_size if page_size > 0 else 0
    
    return {
        "code": 200,
        "message": message,
        "data": {
            "items": items,
            "total": total,
            "page": page,
            "page_size": page_size,
            "total_pages": total_pages
        }
    }

def list_response(items: list, message: str = "获取列表成功") -> Dict[str, Any]:
    """列表响应"""
    return {
        "code": 200,
        "message": message,
        "data": items
    }

def detail_response(item: Any, message: str = "获取详情成功") -> Dict[str, Any]:
    """详情响应"""
    return {
        "code": 200,
        "message": message,
        "data": item
    }

def create_response(item: Any, message: str = "创建成功") -> Dict[str, Any]:
    """创建响应"""
    return {
        "code": 201,
        "message": message,
        "data": item
    }

def update_response(message: str = "更新成功") -> Dict[str, Any]:
    """更新响应"""
    return {
        "code": 200,
        "message": message,
        "data": None
    }

def delete_response(message: str = "删除成功") -> Dict[str, Any]:
    """删除响应"""
    return {
        "code": 200,
        "message": message,
        "data": None
    }

def not_found_response(message: str = "资源不存在") -> Dict[str, Any]:
    """未找到响应"""
    return {
        "code": 404,
        "message": message,
        "data": None
    }

def unauthorized_response(message: str = "未授权访问") -> Dict[str, Any]:
    """未授权响应"""
    return {
        "code": 401,
        "message": message,
        "data": None
    }

def forbidden_response(message: str = "禁止访问") -> Dict[str, Any]:
    """禁止访问响应"""
    return {
        "code": 403,
        "message": message,
        "data": None
    }

def server_error_response(message: str = "服务器内部错误") -> Dict[str, Any]:
    """服务器错误响应"""
    return {
        "code": 500,
        "message": message,
        "data": None
    }
